/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package roles;

import avenue.MyWindowEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.List;

import javax.persistence.EntityManager;


import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellEditor;
import javax.swing.DefaultCellEditor;

import model.DBManager;
import model.Module;
import model.RolePermission;
import model.UserRole;

import users.*;

/**
 *
 * @author Aggelos
 */
public class EditRoleForm3 extends javax.swing.JFrame
{

    EntityManager em;
    UserRole userRoleFromListRolesForm;
    RolePermission rolePermissionFromListRolesForm;
    boolean readOnly;
    boolean initialiseUpdate;
    RolePermission rp22;
    RolePermission rolePermissionFromListRolesForm1;
    RolePermission rolePermissionFromListRolesForm2;
    RolePermission rolePermissionFromListRolesForm3;
    RolePermission rolePermissionFromListRolesForm4;
    RolePermission rolePermissionFromListRolesForm5;
    RolePermission rolePermissionFromListRolesForm6;

    /**
     * Creates new form EditUserForm
     */
    public EditRoleForm3()
    {
        em = DBManager.em;
        initComponents();

        RolePermission rp2 = new RolePermission();
        em.persist(rp2);
        selectPermsByUserRoleLIST.add(rp2);
    }

    public EditRoleForm3(UserRole ur, boolean readOnly)
    {
        em = DBManager.em;

        userRoleFromListRolesForm = ur;
        this.readOnly = readOnly;
        initComponents();
    }

    public EditRoleForm3(UserRole ur2, boolean readOnly, boolean update)
    {
        em = DBManager.em;
        if (!(em.getTransaction().isActive()))
          {
            em.getTransaction().begin();
          }
        initialiseUpdate = update;

        userRoleFromListRolesForm = ur2;
        this.readOnly = readOnly;
        initComponents();
///////////////////////////////////////////////////////////////
//////            RolePermission  rp22 = new RolePermission();
//////            em.persist(rp22);
//////            selectPermsByUserRoleLIST.add(rp22);
///////////////////////////////////////////////////////////////
        if (initialiseUpdate == false)
          {
            rp22 = new RolePermission();
            em.persist(rp22);
            selectPermsByUserRoleLIST.add(rp22);
          }
    }

    public EditRoleForm3(UserRole ur2, RolePermission rp2, boolean readOnly, boolean update)
    {
        em = DBManager.em;
        if (!(em.getTransaction().isActive()))
          {
            em.getTransaction().begin();
          }
        initialiseUpdate = update;


        userRoleFromListRolesForm = ur2;
        rolePermissionFromListRolesForm = rp2;

        this.readOnly = readOnly;
        initComponents();

        if (initialiseUpdate == false)
          {
            em.persist(rolePermissionFromListRolesForm);
            selectPermsByUserRoleLIST.add(rolePermissionFromListRolesForm);
          }
    }

    public EditRoleForm3(UserRole ur2, RolePermission rp1,
            RolePermission rp2, RolePermission rp3,
            RolePermission rp4, RolePermission rp5,
            RolePermission rp6, boolean readOnly, boolean update)
    {
        em = DBManager.em;
        if (!(em.getTransaction().isActive()))
          {
            em.getTransaction().begin();
          }
        initialiseUpdate = update;


        userRoleFromListRolesForm = ur2;
        rolePermissionFromListRolesForm1 = rp1;
        rolePermissionFromListRolesForm2 = rp2;
        rolePermissionFromListRolesForm3 = rp3;
        rolePermissionFromListRolesForm4 = rp4;
        rolePermissionFromListRolesForm5 = rp5;
        rolePermissionFromListRolesForm6 = rp6;

        this.readOnly = readOnly;
        initComponents();

        if (initialiseUpdate == false)
          {
            em.persist(rolePermissionFromListRolesForm1);
            em.persist(rolePermissionFromListRolesForm2);
            em.persist(rolePermissionFromListRolesForm3);
            em.persist(rolePermissionFromListRolesForm4);
            em.persist(rolePermissionFromListRolesForm5);
            em.persist(rolePermissionFromListRolesForm6);
            selectPermsByUserRoleLIST.add(rolePermissionFromListRolesForm1);
            selectPermsByUserRoleLIST.add(rolePermissionFromListRolesForm2);
            selectPermsByUserRoleLIST.add(rolePermissionFromListRolesForm3);
            selectPermsByUserRoleLIST.add(rolePermissionFromListRolesForm4);
            selectPermsByUserRoleLIST.add(rolePermissionFromListRolesForm5);
            selectPermsByUserRoleLIST.add(rolePermissionFromListRolesForm6);
          }

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents()
    {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        userRoleGuiObject = userRoleFromListRolesForm;
        selectPermsByUserRoleQUERY = em.createQuery("select rp from RolePermission rp where rp.roleId=:userRole").setParameter("userRole",userRoleGuiObject);
        selectPermsByUserRoleLIST = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(selectPermsByUserRoleQUERY.getResultList());
        moduleQuery = java.beans.Beans.isDesignTime() ? null : em.createQuery("select f from Module f");
        moduleList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(moduleQuery.getResultList());
        modRend = new renderers.moduleComboBoxRenderer();
        moduleTCR = new renderers.TableCellModulesRenderer();
        userRolesTCR = new renderers.TableCellUserRolesRenderer();
        jLabel1 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        roleNameTextField = new javax.swing.JTextField();
        saveButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        idRoleLBL = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jSeparator4 = new javax.swing.JSeparator();
        jScrollPane2 = new javax.swing.JScrollPane();
        userTable = new javax.swing.JTable();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        canDeleteCheckBox = new javax.swing.JCheckBox();
        canUpdateCheckBox = new javax.swing.JCheckBox();
        StationLevelAccessCheckBox = new javax.swing.JCheckBox();
        canRetrieveCheckBox = new javax.swing.JCheckBox();
        canCreateCheckBox = new javax.swing.JCheckBox();
        UpdateSelectionsButton = new javax.swing.JButton();
        modsCB = new javax.swing.JComboBox();
        deletePermissions = new javax.swing.JButton();

        moduleTCR.setText("tableCellModulesRenderer1");

        userRolesTCR.setText("tableCellUserRolesRenderer1");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel1.setText("Όνομα Ρόλου : ");

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, userRoleGuiObject, org.jdesktop.beansbinding.ELProperty.create("${roleName}"), roleNameTextField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        saveButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/save.gif"))); // NOI18N
        saveButton.setText("Αποθήκευση");
        saveButton.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                saveButtonActionPerformed(evt);
            }
        });

        cancelButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/cancel.gif"))); // NOI18N
        cancelButton.setText("Ακυρο");
        cancelButton.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                cancelButtonActionPerformed(evt);
            }
        });

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, userRoleGuiObject, org.jdesktop.beansbinding.ELProperty.create("${id}"), idRoleLBL, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 20)); // NOI18N
        jLabel2.setText("Διαχείριση Ρόλου");

        userTable.getTableHeader().setReorderingAllowed(false);

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, selectPermsByUserRoleLIST, userTable);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${roleId}"));
        columnBinding.setColumnName("Ρόλος");
        columnBinding.setColumnClass(model.UserRole.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${moduleId}"));
        columnBinding.setColumnName("Module");
        columnBinding.setColumnClass(model.Module.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${canCreate}"));
        columnBinding.setColumnName("(C)reate");
        columnBinding.setColumnClass(Integer.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${canRetrieve}"));
        columnBinding.setColumnName("(R)etrieve");
        columnBinding.setColumnClass(Integer.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${canUpdate}"));
        columnBinding.setColumnName("(U)pdate");
        columnBinding.setColumnClass(Integer.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${canDelete}"));
        columnBinding.setColumnName("(D)elete");
        columnBinding.setColumnClass(Integer.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${stationLevelAccess}"));
        columnBinding.setColumnName("Station Level Access");
        columnBinding.setColumnClass(Integer.class);
        columnBinding.setEditable(false);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        jScrollPane2.setViewportView(userTable);
        userTable.getColumnModel().getColumn(0).setCellRenderer(userRolesTCR);
        userTable.getColumnModel().getColumn(1).setCellRenderer(moduleTCR);

        jTabbedPane1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        jTabbedPane1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        canDeleteCheckBox.setText(" Delete");

        canUpdateCheckBox.setText("Update");

        StationLevelAccessCheckBox.setText("STATION LEVEL ACCESS");

        canRetrieveCheckBox.setText("Retrieve");

        canCreateCheckBox.setText("Create");

        UpdateSelectionsButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Refresh.png"))); // NOI18N
        UpdateSelectionsButton.setText("Ανανέωση");
        UpdateSelectionsButton.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                UpdateSelectionsButtonActionPerformed(evt);
            }
        });

        modsCB.setRenderer(modRend);

        org.jdesktop.swingbinding.JComboBoxBinding jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, moduleList, modsCB);
        bindingGroup.addBinding(jComboBoxBinding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, moduleList, org.jdesktop.beansbinding.ObjectProperty.create(), modsCB, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"));
        bindingGroup.addBinding(binding);

        deletePermissions.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Trash.png"))); // NOI18N
        deletePermissions.setText("Διαγραφή δικαιωμάτων");
        deletePermissions.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                deletePermissionsActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(modsCB, 0, 205, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(canCreateCheckBox)
                .addGap(18, 18, 18)
                .addComponent(canRetrieveCheckBox)
                .addGap(18, 18, 18)
                .addComponent(canUpdateCheckBox)
                .addGap(18, 18, 18)
                .addComponent(canDeleteCheckBox)
                .addGap(26, 26, 26)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(StationLevelAccessCheckBox)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(deletePermissions, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(UpdateSelectionsButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(canCreateCheckBox)
                    .addComponent(canRetrieveCheckBox)
                    .addComponent(canUpdateCheckBox)
                    .addComponent(canDeleteCheckBox)
                    .addComponent(StationLevelAccessCheckBox)
                    .addComponent(modsCB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(UpdateSelectionsButton)
                .addGap(18, 18, 18)
                .addComponent(deletePermissions)
                .addContainerGap(36, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Διαχείριση Δικαιωμάτων", jPanel1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jSeparator4)
                    .addComponent(jScrollPane2)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(roleNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(39, 39, 39)
                                .addComponent(idRoleLBL))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(saveButton)
                                .addGap(19, 19, 19)
                                .addComponent(cancelButton))
                            .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 743, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 151, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addGap(13, 13, 13)
                .addComponent(jSeparator4, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(roleNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(idRoleLBL))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 13, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 43, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelButton)
                    .addComponent(saveButton))
                .addContainerGap())
        );

        jTabbedPane1.getAccessibleContext().setAccessibleName("");
        jTabbedPane1.getAccessibleContext().setAccessibleDescription("");

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        // TODO add your handling code here:
        MyWindowEvent we = new MyWindowEvent(this, WindowEvent.WINDOW_CLOSED, false);
        for (WindowListener l : this.getWindowListeners())
          {
            l.windowClosed(we);
          }
        this.setVisible(false);
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
        // TODO add your handling code here:
        MyWindowEvent we = new MyWindowEvent(this, WindowEvent.WINDOW_CLOSED, true);
        for (WindowListener l : this.getWindowListeners())
          {
            l.windowClosed(we);
          }
        this.setVisible(false);
    }//GEN-LAST:event_saveButtonActionPerformed

    private void UpdateSelectionsButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_UpdateSelectionsButtonActionPerformed
    {//GEN-HEADEREND:event_UpdateSelectionsButtonActionPerformed
        // TODO add your handling code here:

        if (roleNameTextField.getText().isEmpty())
          {
            JOptionPane.showMessageDialog(this, "please enter name");
          }
        else
          {
            rightsAdministration(modsCB.getSelectedIndex());

            userRoleFromListRolesForm.setRoleName(roleNameTextField.getText());
            userTable.getModel().setValueAt(userRoleFromListRolesForm, 0, 0);
            userTable.getModel().setValueAt(userRoleFromListRolesForm, 1, 0);
            userTable.getModel().setValueAt(userRoleFromListRolesForm, 2, 0);
            userTable.getModel().setValueAt(userRoleFromListRolesForm, 3, 0);
            userTable.getModel().setValueAt(userRoleFromListRolesForm, 4, 0);
            userTable.getModel().setValueAt(userRoleFromListRolesForm, 5, 0);

            userTable.repaint();
          }
    }//GEN-LAST:event_UpdateSelectionsButtonActionPerformed

    private void deletePermissionsActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_deletePermissionsActionPerformed
    {//GEN-HEADEREND:event_deletePermissionsActionPerformed
        // TODO add your handling code here:
        String str = "Αυτή η επιλογή θα αφαιρέσει τα δικαιώματα του χρηστή οριστικά\n"
                + "και θα επιστρέψει στην φόρμα 'Πίνακας Ρόλων'\n Συνέχεια?";

        int n = JOptionPane.showConfirmDialog(this,
                str, "Διαγραφή Δικαιωμάτων", JOptionPane.YES_NO_OPTION);

        if (n == 0)//yes
          {
            try
              {
                for (int i = 0; i < selectPermsByUserRoleLIST.size(); i++)
                  {
                    deleteRolePermissionByUserRole(selectPermsByUserRoleLIST.get(i).getRoleId());
                  }
                selectPermsByUserRoleLIST.clear();
                JOptionPane.showMessageDialog(this, "list empty");
              }
            catch (Exception e)
              {
                JOptionPane.showMessageDialog(this, "Error");
              }

          }
        
         MyWindowEvent we = new MyWindowEvent(this, WindowEvent.WINDOW_CLOSED, false);
        for (WindowListener l : this.getWindowListeners())
          {
            l.windowClosed(we);
          }
        this.setVisible(false);


    }//GEN-LAST:event_deletePermissionsActionPerformed

    public void rightsAdministration(int row)
    {
        if (canCreateCheckBox.isSelected())
          {
            userTable.getModel().setValueAt(1, row, 2);
          }
        if (!canCreateCheckBox.isSelected())
          {
            userTable.getModel().setValueAt(0, row, 2);
          }

//////////////////////////////////////////

        if (canRetrieveCheckBox.isSelected())
          {
            userTable.getModel().setValueAt(1, row, 3);
          }
        if (!canRetrieveCheckBox.isSelected())
          {
            userTable.getModel().setValueAt(0, row, 3);
          }

//////////////////////////////////////////
        if (canUpdateCheckBox.isSelected())
          {
            userTable.getModel().setValueAt(1, row, 4);
          }
        if (!canUpdateCheckBox.isSelected())
          {
            userTable.getModel().setValueAt(0, row, 4);
          }
        //////////////////////////////////////////           
        if (canDeleteCheckBox.isSelected())
          {
            userTable.getModel().setValueAt(1, row, 5);
          }
        if (!canDeleteCheckBox.isSelected())
          {
            userTable.getModel().setValueAt(0, row, 5);
          }
        //////////////////////////////////////////  
        if (StationLevelAccessCheckBox.isSelected())
          {
            userTable.getModel().setValueAt(1, row, 6);
          }
        if (!StationLevelAccessCheckBox.isSelected())
          {
            userTable.getModel().setValueAt(0, row, 6);
          }
    }

    public List<RolePermission> retrieveRolePermisionByUserRole(UserRole user_role_R)
    {
        String idS = user_role_R.getId().toString();
        String strSQL = "select * from ROLE_PERMISSION where ROLE_PERMISSION.ROLE_ID = " + idS;

        List<RolePermission> urList = em.createNativeQuery(strSQL,
                RolePermission.class).getResultList();
        return urList;
    }

    public void deleteRolePermissionByUserRole(UserRole user_role)
    {
        if (!(em.getTransaction().isActive()))
          {
            em.getTransaction().begin();
          }
        RolePermission rTemp = retrieveRolePermisionByUserRole(user_role).get(0);
        em.remove(rTemp);
        em.getTransaction().commit();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[])
    {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try
          {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels())
              {
                if ("Nimbus".equals(info.getName()))
                  {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                  }
              }
          }
        catch (ClassNotFoundException ex)
          {
            java.util.logging.Logger.getLogger(EditUserForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
          }
        catch (InstantiationException ex)
          {
            java.util.logging.Logger.getLogger(EditUserForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
          }
        catch (IllegalAccessException ex)
          {
            java.util.logging.Logger.getLogger(EditUserForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
          }
        catch (javax.swing.UnsupportedLookAndFeelException ex)
          {
            java.util.logging.Logger.getLogger(EditUserForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
          }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                new EditRoleForm3().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox StationLevelAccessCheckBox;
    private javax.swing.JButton UpdateSelectionsButton;
    private javax.swing.JCheckBox canCreateCheckBox;
    private javax.swing.JCheckBox canDeleteCheckBox;
    private javax.swing.JCheckBox canRetrieveCheckBox;
    private javax.swing.JCheckBox canUpdateCheckBox;
    private javax.swing.JButton cancelButton;
    private javax.swing.JButton deletePermissions;
    private javax.swing.JLabel idRoleLBL;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator4;
    private javax.swing.JTabbedPane jTabbedPane1;
    private renderers.moduleComboBoxRenderer modRend;
    private javax.swing.JComboBox modsCB;
    private java.util.List<Module> moduleList;
    private javax.persistence.Query moduleQuery;
    private renderers.TableCellModulesRenderer moduleTCR;
    private javax.swing.JTextField roleNameTextField;
    private javax.swing.JButton saveButton;
    private java.util.List<RolePermission> selectPermsByUserRoleLIST;
    private javax.persistence.Query selectPermsByUserRoleQUERY;
    private model.UserRole userRoleGuiObject;
    private renderers.TableCellUserRolesRenderer userRolesTCR;
    private javax.swing.JTable userTable;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
}
